import { Component, OnInit } from '@angular/core';
import { HttpClient,HttpHeaders} from '@angular/common/http';
import { FixedSizeVirtualScrollStrategy } from '@angular/cdk/scrolling';

@Component({
  selector: 'app-body',
  templateUrl: './body.component.html',
  styleUrls: ['./body.component.scss']
})
export class BodyComponent implements OnInit {
    public username:any = ''
    public password:any = ''
    public isShowLogin:any = false
    public isShowUsers:any = true
    public isShowTags:any = true
    public isSingup:any = true
    public users:any[]=[];
    public tags:any[]=[];
    public newusername:any = ''
    public newemail:any = ''
    public newpassword:any = ''
    public newfirstname:any = ''
    public newlastname:any = ''

    myHttpHead = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) };
    constructor (public http:HttpClient){}

    newuser():void{
        let url = 'http://localhost:3001/user/create';
        if(this.newemail =='' || this.newfirstname == '' || this.newlastname == '' || this.newpassword == '' || this.newusername==''){
            alert('所有信息必填');
        }else{
            this.http.post(url,{'username':this.newusername,'password':this.newpassword,'lastname':this.newlastname,'firstname':this.newfirstname,'email':this.newemail},this.myHttpHead).subscribe(
                (val) => {
                    console.log(val);
                },
                response =>{
                    if(response.error.text!='success'){
                        alert(response.error.text);
                    }else{
                        this.isShowLogin = true;
                        this.isShowTags = false;
                        this.isShowUsers = false;
                    }
                },
                ()=>{
                    console.log(111);
                }
            );
        }
    }

    deleteuser(id):void{
        let url = 'http://localhost:3001/user/delete';
        this.http.post(url,{'id':id},this.myHttpHead).subscribe(
            (val) => {
                console.log(val);
            },
            response =>{
                if(response.error.text!='success'){
                    alert(response.error.text);
                }else{
                    // this.isShowLogin = true;
                    // this.isShowTags = false;
                    // this.isShowUsers = false;
                    alert('删除成功');
                }
            },
            ()=>{
                console.log(111);
            }
        );
    }

    deletetag(id):void{
        let url = 'http://localhost:3001/tags/delete';
        this.http.post(url,{'id':id},this.myHttpHead).subscribe(
            (val) => {
                console.log(val);
            },
            response =>{
                if(response.error.text!='success'){
                    alert(response.error.text);
                }else{
                    // this.isShowLogin = true;
                    // this.isShowTags = false;
                    // this.isShowUsers = false;
                    alert('删除成功');
                }
            },
            ()=>{
                console.log(111);
            }
        );
    }

    ngOnInit(): void {
        let url = 'http://localhost:3001/user/list';
        this.http.get(url).subscribe((response:any)=>{
            this.users = response;
        });

        url = 'http://localhost:3001/tags/list';
        this.http.get(url).subscribe((response:any)=>{
            this.tags = response;
        });
    }
    singup():void{
        this.isShowLogin = true;
        this.isShowTags = true;
        this.isShowUsers = true;
        this.isSingup = false;
    }
    submit():void{
        if(this.username=='' || this.password==''){
            alert('输入用户名和密码');
        }else{
            let url = 'http://localhost:3001/user/login';
            this.http.post(url,{'username':this.username,'password':this.password},this.myHttpHead).subscribe(
                (val) => {
                    console.log(val);
                },
                response =>{
                    if(response.error.text!='success'){
                        alert(response.error.text);
                    }else{
                        this.isShowLogin = true;
                        this.isShowTags = false;
                        this.isShowUsers = false;
                    }
                },
                ()=>{
                    console.log(111);
                }
            );
        }
    }
}
